﻿using System;
using System.Drawing;
using System.Windows.Forms;
using AutoduelClient.GameClient.Player;
using Microsoft.Xna.Framework;

namespace AutoduelClient.GameUI
{
    public delegate void MenuChoiceCallback(object sender, MenuChoiceEventArgs e);
    
    public partial class MenuChoiceDisplayer : Form
    {

        private GameClient.Player.MenuChoice menuChoice;

        public GameClient.Player.MenuChoice MenuChoice
        {
            get { return menuChoice; }
            set { menuChoice = value; }
        }

        public MenuChoiceDisplayer()
        {
            InitializeComponent();
        }

        public MenuChoiceDisplayer(GameClient.Player.MenuChoice menuChoice)
        {
            InitializeComponent();

            MenuChoice newMenuChoice = new MenuChoice(menuChoice.menuChoiceCallback, menuChoice.choice, menuChoice.data, menuChoice.needsInput);


            newMenuChoice.choice = menuChoice.choice;
            newMenuChoice.menuChoiceCallback = menuChoice.menuChoiceCallback;
            newMenuChoice.needsInput = menuChoice.needsInput;

            if (menuChoice.data != null)
            {
                newMenuChoice.data = new object[menuChoice.data.Length];

                // other arguments need to be determined dynamically
                for (int i = 0; i < menuChoice.data.Length; i++)
                {
                    // used for city bus
                    if ((Type)menuChoice.data[i] == typeof(Coordinate))
                    {
                        newMenuChoice.data[i] = new Coordinate();

                        // create labels to guide user input
                        Label lb1 = new Label();
                        Label lb2 = new Label();

                        // set dimensions
                        lb1.Width = 75;
                        lb1.Height = 20;
                        lb2.Width = 75;
                        lb2.Height = 20;

                        // set location
                        lb1.Location = new System.Drawing.Point(this.Location.X, this.Location.Y + 5);
                        lb2.Location = new System.Drawing.Point(lb1.Location.X, lb1.Location.Y + lb1.Height + 5);

                        // set text
                        lb1.Text = "Enter Point X: ";
                        lb2.Text = "Enter Point Y: ";

                        // add the labels to the form
                        this.Controls.Add(lb1);
                        this.Controls.Add(lb2);

                        // create textboxes for inputting a point
                        TextBox tb1 = new TextBox();
                        TextBox tb2 = new TextBox();

                        // set dimensions
                        tb1.Width = this.Width - lb1.Width - 25;
                        tb1.Height = lb1.Height;
                        tb2.Width = this.Width - lb2.Width - 25;
                        tb1.Height = lb2.Height;

                        // set location
                        tb1.Location = new System.Drawing.Point(lb1.Location.X + lb1.Width + 5, this.Location.Y + 5);
                        tb2.Location = new System.Drawing.Point(lb2.Location.X + lb2.Width + 5, tb1.Location.Y + tb1.Height + 5);

                        // tag the textboxes with the current index
                        tb1.Tag = i;
                        tb2.Tag = i;

                        // add the textboxes to the form
                        this.Controls.Add(tb1);
                        this.Controls.Add(tb2);
                    }
                }
                this.menuChoice = newMenuChoice;

                if (!newMenuChoice.needsInput)
                {
                    okButton_Click(this, EventArgs.Empty);
                }
            }
            // if there is no data, assume an ok button click
            else
            {
                this.menuChoice = newMenuChoice;
                okButton_Click(this, EventArgs.Empty);
            }
        }

        private void okButton_Click(object sender, EventArgs e)
        {
            if (this.menuChoice.data != null)
            {
                int[] occurrences = new int[menuChoice.data.Length];
                Coordinate p = new Coordinate();

                foreach (Control control in this.Controls)
                {
                    if (control.Tag != null && control.Tag.GetType() == typeof(int))
                    {
                        int i = (int)control.Tag;

                        if (menuChoice.data[i].GetType() == typeof(Coordinate))
                        {
                            try
                            {
                                if (occurrences[i] == 0)
                                { 
                                    p.X = Int32.Parse(((TextBox)control).Text);
                                }
                                else if (occurrences[i] == 1)
                                {
                                    p.Y = Int32.Parse(((TextBox)control).Text);
                                }
                            }
                            catch (Exception ex)
                            {
                                this.Enabled = false;
                                new AsyncMessageBox(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error).Show(this);
                                return;
                            }
                        }
                        occurrences[i]++;
                    }
                }

                for (int i = 0; i < menuChoice.data.Length; i++)
                {
                    if ((Type)menuChoice.data[i].GetType() == typeof(Coordinate))
                    {
                        menuChoice.data[i] = p;
                    }
                }
            }
            menuChoice.menuChoiceCallback(this, new MenuChoiceEventArgs(this.menuChoice));
            this.Close();
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void MenuChoiceDisplayer_Load(object sender, EventArgs e)
        {
            this.Location = new System.Drawing.Point(this.Owner.Location.X + this.Owner.Width / 2 - this.Width / 2, this.Owner.Location.Y + this.Owner.Height / 2 - this.Height / 2);
        }

        private void MenuChoiceDisplayer_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Owner.Enabled = true;
        }
    }
}
